package com.yxr.affairs;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.yxr.affairs.api.model.ResultEntity;
import com.yxr.affairs.api.model.child.Login;
import com.yxr.affairs.base.BaseSwipeBackActivity;

/**
 * Created by chengzhuhai on 2017/11/10.
 */

public class ZJWebViewActivity extends BaseSwipeBackActivity{
    private WebView mWebView;
    private ProgressBar mProgressBar;

    private static final String EXIT_1 = "?u=reg";//中金注册
    private static final String EXIT_2 = "?u=bindcard";//绑定银行卡
    private static final String EXIT_3 = "?u=qianyue";//签约

    private String flag = null;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_zjwebview;
    }

    @Override
    protected void initViews() {

        ImageButton mBack = (ImageButton) findViewById(R.id.ib_supportbank_back);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exit();
            }
        });

        TextView titleTv = (TextView) findViewById(R.id.tv_supportbank_title);
        Intent data = getIntent();
        String title = data.getStringExtra("title");
        titleTv.setText(title);
        String url = data.getStringExtra("url");
        try {
            flag = data.getStringExtra("flag");
        } catch (Exception e) {

        }

        mProgressBar = (ProgressBar) findViewById(R.id.pb_supportbank);
        mWebView = (WebView)findViewById(R.id.wv_supportbank_webview);
        WebSettings webSettings = mWebView.getSettings();

        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setSavePassword(false);

        webSettings.setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(this, "ZJWebViewActivity");
        mWebView.setWebViewClient(new MyWebViewClient());

        mWebView.loadData(url, "text/html", "UTF-8");

    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if (url.contains(EXIT_1) || url.contains(EXIT_2) || url.contains(EXIT_3)) {
                exit();
            }

            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

            if (url.contains(EXIT_1) || url.contains(EXIT_2) || url.contains(EXIT_3)) {
                exit();
            }

            mProgressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            if (url.contains(EXIT_1) || url.contains(EXIT_2) || url.contains(EXIT_3)) {
                exit();
            }

            mProgressBar.setVisibility(View.GONE);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);

            if (failingUrl.contains(EXIT_1) || failingUrl.contains(EXIT_2) || failingUrl.contains(EXIT_3)) {
                exit();
            }

            mProgressBar.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void getFinish(){
        if (mWebView.canGoBack()){
            mWebView.goBack();
            return;
        }
        exit();
    }

    @Override
    public void onBackPressed() {
        return;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            getFinish();
        }
        return super.onKeyDown(keyCode, event);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    @JavascriptInterface
    public void jsCallback(String value){
        if(value.equals("success")){
            exit();
        }
    }

    private void exit(){
        if (!TextUtils.isEmpty(flag)) {
            if (flag.equals("ca")) {//充值
                Intent myIntent = new Intent();
                myIntent.setAction("com.app.ChargeActivity");
                sendBroadcast(myIntent);
            }else if (flag.equals("ema")) {//提现
                Intent myIntent = new Intent();
                myIntent.setAction("com.app.ExtractMoneyActivity");
                sendBroadcast(myIntent);
            }else if (flag.equals("ma")) {//首页
                Intent myIntent = new Intent();
                myIntent.setAction("com.app.MainActivity");
                myIntent.putExtra("data", "login");
                sendBroadcast(myIntent);
            }else if (flag.equals("aba")) {//智能投资签约
               // setResult(AutomaticBidActivity.RESULT_CODE, new Intent(mContext, AutomaticBidActivity.class));
            }
        }
        finish();
    }



    @Override
    protected void updateViews(boolean isRefresh) {

    }




    @Override
    protected void onError_(Throwable e) {

    }

    @Override
    protected void onCancel_() {

    }
}
